package com.fangs.yehwangglobal.net;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import com.fangs.yehwangglobal.base.AccountService;
import com.fangs.yehwangglobal.base.BaseResultEntity;
import com.fangs.yehwangglobal.base.SessionModel;
import com.fangs.yehwangglobal.base.YehApp;
import com.fangs.yehwangglobal.model.CartNumEntity;
import com.fangs.yehwangglobal.model.LoginRefreshEntity;
import com.fangs.yehwangglobal.widget.eventbus.EventManager;
import com.fangs.yehwangglobal.widget.loading.LoadIngDialog;

import org.jetbrains.annotations.NotNull;

import java.lang.ref.WeakReference;

import io.reactivex.observers.DefaultObserver;

/**
 * Created by Administrator on 2018/1/29.
 */
public class ProgressObserver<T> extends DefaultObserver<T> {
    //    回调接口
    private final BaseObserver<T> mSubscriberOnNextListener;
    //    弱引用反正内存泄露
    private final WeakReference<Context> mActivity;
    //    是否显示dialog
    private boolean showDialog = true;
    //    加载框可自己定义
    private LoadIngDialog mLoadIngDialog;

    public ProgressObserver(@NonNull @NotNull BaseObserver<T> mSubscriberOnNextListener, Context context) {
        this.mSubscriberOnNextListener = mSubscriberOnNextListener;
        this.mActivity = new WeakReference<>(context);
        initProgressDialog();
    }

    public ProgressObserver(@NonNull @NotNull BaseObserver<T> mSubscriberOnNextListener, Context context, boolean showDialog) {
        this.mSubscriberOnNextListener = mSubscriberOnNextListener;
        this.mActivity = new WeakReference<>(context);
        this.showDialog = showDialog;
        initProgressDialog();
    }

    /**
     * 初始化加载框
     */
    private void initProgressDialog() {
        Context context = mActivity.get();
        if (showDialog) {
            mLoadIngDialog = new LoadIngDialog(context);
        }
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void onNext(@NotNull T t) {
        if (!isFinishActivity() && (t instanceof BaseResultEntity)) {
            BaseResultEntity resultEntity = (BaseResultEntity) t;
            if (resultEntity.isOut()) {//退出重新登录
                AccountService accountService = YehApp.getInstance().accountService();
                accountService.updateAccount(new SessionModel(accountService.email(), accountService.password(), "",
                        accountService.language(), accountService.currency()));
                EventManager.postSticky(new CartNumEntity(0));
                EventManager.post(new LoginRefreshEntity(false));
//                mActivity.get().startActivity(new Intent(mActivity.get(), LoginActivity.class));
//                mActivity.get().startActivity(new Intent(mActivity.get(), LoginWebViewActivity.class));
            } else {
                mSubscriberOnNextListener.onSuccess(t);
            }
        }

        dismissProgressDialog();
    }

    @Override
    public void onError(@NotNull Throwable t) {
        if (!isFinishActivity()) {
            mSubscriberOnNextListener.onFail(t);
        }
        dismissProgressDialog();
    }

    @Override
    public void onComplete() {
        dismissProgressDialog();
    }

    @Override
    protected void onStart() {
        super.onStart();
        showProgressDialog();
    }

    /**
     * 显示加载框
     */
    private void showProgressDialog() {
        if (!isFinishActivity() && mLoadIngDialog != null) {
            try {
                mLoadIngDialog.showNow();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 隐藏
     */
    private void dismissProgressDialog() {
        if (!isFinishActivity() && mLoadIngDialog != null) {
            try {
                mLoadIngDialog.dismissNow();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean isFinishActivity() {
        Context context = mActivity.get();
        return context == null || ((context instanceof Activity) && ((Activity) context).isFinishing());
    }
}
